[PR]

『新妻LOVELY×CATION』を応援しています!
水無瀬の部屋 > Programming > sample > tools > misc > sha512.cpp
最終更新日: 2007/03/04

   1: //*********************************************************
   2: // プロジェクト: TOOLS::SHA512 - Secure Hash Algorithm
   3: //   ファイル名: sha512.cpp
   4: //*********************************************************
   5: #include <misc/sha512.h>     //
   6: #include <header/tooldbg.h>  // ASSERT(), 
   7: #include <header/toolbase.h> // 
   8: #include <header/snprintf.h> // snprintf(), vsnprintf(), 
   9: #include <limits.h>          // CHAR_BIT, 
  10: 
  11: 
  12: //---------------------------------------------------------
  13: // テスト関数 の 宣言
  14: //---------------------------------------------------------
  15: DECLARE_TESTPROC( test_sha512 );
  16: 
  17: 
  18: //---------------------------------------------------------
  19: // 定数型マクロ の 定義
  20: //---------------------------------------------------------
  21: #define DBG_ALLOCNAME  "SHA512_CreateAlgorithm"
  22: 
  23: //
  24: #define SHA512_HASH    8
  25: #define SHA512_BLOCK  16
  26: #define SHA512_WORK   80
  27: #define SHA512_BUFFER_SIZE  (SHA512_BLOCK * 8)
  28: 
  29: 
  30: //---------------------------------------------------------
  31: // 構造体 の 宣言
  32: //---------------------------------------------------------
  33: typedef struct SHA512_CTX_tag
  34: {
  35: 	//
  36: 	DWORD64 length;     // 総書込バイト数
  37: 	DWORD64 state[ 8 ]; // 
  38: 
  39: 	// 
  40: 	DWORD   index;        // バッファの最終書込バイト位置
  41: 	DWORD64 buffer[ 16 ]; // 
  42: } SHA512_CTX;
  43: 
  44: 
  45: //---------------------------------------------------------
  46: // ファイルスコープ関数 の 宣言
  47: //---------------------------------------------------------
  48: static inline DWORD ReverseEndian(DWORD x) {return (x << 24) | ((x & 0x0000ff00) << 8) | ((x & 0x00ff0000) >> 8) | (x >> 24);}
  49: static inline DWORD64 ReverseEndian64(DWORD64 x){return ((DWORD64)ReverseEndian((DWORD)(x & 0x00000000ffffffff)) << 32) | ReverseEndian((DWORD)(x >> 32));}
  50: static void SHA512_Transform( DWORD64 *state, const DWORD64 *buffer );
  51: static bool SHA512_IsValidContext( const SHA512_CTX *context );
  52: 
  53: 
  54: //*********************************************************
  55: // SHA512_CreateAlgorithm
  56: //*********************************************************
  57: SHA512_CTX *
  58: SHA512_CreateAlgorithm
  59: 	(
  60: 		void
  61: 	)
  62: {
  63: 	CALLONCE_TESTPROC( test_sha512 ); // [テスト]
  64: 
  65: 	SHA512_CTX *context = (SHA512_CTX *)malloc( sizeof( *context ) );
  66: 	if ( !context )
  67: 		return null;
  68: 
  69: 	SHA512_InitData( context );
  70: 	ASSERT( SHA512_IsValidContext( context ) );
  71: 	
  72: 	DBG_LOCK_ALLOCED_MEMORY( context, DBG_ALLOCNAME ); // [DBG]
  73: 	return context;
  74: }//SHA512_CreateAlgorithm
  75: 
  76: //*********************************************************
  77: // SHA512_DestroyAlgorithm
  78: //*********************************************************
  79: bool
  80: SHA512_DestroyAlgorithm
  81: 	(
  82: 		SHA512_CTX *context
  83: 	)
  84: {
  85: 	// パラメタの仮定
  86: 	ASSERT( SHA512_IsValidContext( context ) );
  87: 
  88: 	DBG_UNLOCK_ALLOCED_MEMORY( context, DBG_ALLOCNAME ); // [DBG]
  89: 	free( context );
  90: 
  91: 	return true;
  92: }//SHA512_DestroyAlgorithm
  93: 
  94: //*********************************************************
  95: // SHA512_InitData
  96: //*********************************************************
  97: bool
  98: SHA512_InitData
  99: 	(
 100: 		SHA512_CTX *context
 101: 	)
 102: {
 103: 	// パラメタの仮定
 104: 	ASSERT( IsValidPtr( context, sizeof( *context ) ) );
 105: 
 106: 	context->index  = 0;
 107: 	context->length = 0;
 108: 
 109: 	const DWORD64 state[] =
 110: 		{
 111: 			0x6a09e667f3bcc908,
 112: 			0xbb67ae8584caa73b,
 113: 			0x3c6ef372fe94f82b,
 114: 			0xa54ff53a5f1d36f1,
 115: 			0x510e527fade682d1,
 116: 			0x9b05688c2b3e6c1f,
 117: 			0x1f83d9abfb41bd6b,
 118: 			0x5be0cd19137e2179,
 119: 		};
 120: 	memzero( context->buffer, sizeof( context->buffer ) );
 121: 	memcpy( context->state, state, sizeof( context->state ) );
 122: 
 123: 	ASSERT( SHA512_IsValidContext( context ) );
 124: 	return true;
 125: }//SHA512_InitData
 126: 
 127: //*********************************************************
 128: // SHA512_AddData
 129: //*********************************************************
 130: bool
 131: SHA512_AddData
 132: 	(
 133: 		      SHA512_CTX *context,
 134: 		const void       *data,
 135: 		      int         length
 136: 	)
 137: {
 138: 	// パラメタの仮定
 139: 	ASSERT( SHA512_IsValidContext( context ) );
 140: 	ASSERT( 0 < length );
 141: 	ASSERT( IsValidReadPtr( data, length ) );
 142: 
 143: 	const BYTE *p = (BYTE *)data;
 144: 	while( 0 < length )
 145: 	{
 146: 		const DWORD use = min( ((DWORD)length), (sizeof( context->buffer ) - context->index) );
 147: 		memcpy( ((BYTE *)context->buffer) + context->index, p, use );
 148: 		p               += use;
 149: 		context->index  += use;
 150: 		context->length += use;
 151: 		length          -= use;
 152: 
 153: 		if ( sizeof( context->buffer ) <= context->index )
 154: 		{
 155: 			SHA512_Transform( context->state, context->buffer );
 156: 			context->index = 0;
 157: 		}
 158: 	}
 159: 	return true;
 160: }//SHA512_AddData
 161: 
 162: //*********************************************************
 163: // SHA512_GetHash
 164: //*********************************************************
 165: bool
 166: SHA512_GetHash
 167: 	(
 168: 		const SHA512_CTX *context,
 169: 		      void       *digest,
 170: 		      int         bufsize 
 171: 	)
 172: {
 173: 	// パラメタの仮定
 174: 	ASSERT( SHA512_IsValidContext( context ) );
 175: 	ASSERT( 0 < bufsize );
 176: 	ASSERT( IsValidPtr( digest, bufsize ) );
 177: 	DESTROY_BUFFER( digest, bufsize );
 178: 
 179: 	SHA512_CTX tmp_context;
 180: 	memcpy( &tmp_context, context, sizeof( *context ) );
 181: 	context = null;
 182: 
 183: 	DWORD64 qwNumBits[ 2 ]; 
 184: 	qwNumBits[ 0 ] = ReverseEndian64( (tmp_context.length >> 61) );
 185: 	qwNumBits[ 1 ] = ReverseEndian64( (tmp_context.length <<  3) );
 186: 
 187: 	BYTE cOne  = 0x80;
 188: 	SHA512_AddData( &tmp_context, &cOne, 1 );
 189: 	while( tmp_context.index != sizeof( tmp_context.buffer ) - 16 ) 
 190: 	{
 191: 		BYTE cZero = 0x00;
 192: 		SHA512_AddData( &tmp_context, &cZero, 1 );
 193: 	}
 194: 
 195: 	SHA512_AddData( &tmp_context, qwNumBits, 16 );
 196: 
 197: 	memmove( digest, tmp_context.state, min( bufsize, sizeof( tmp_context.state ) ) );
 198: 	return true;
 199: }//SHA512_GetHash
 200: 
 201: //*********************************************************
 202: // SHA512_GetHashText
 203: //*********************************************************
 204: bool
 205: SHA512_GetHashText
 206: 	(
 207: 		const SHA512_CTX *context,
 208: 		      char       *buffer,
 209: 			  int         bufsize
 210: 	)
 211: {
 212: 	// パラメタの仮定
 213: 	ASSERT( SHA512_IsValidContext( context ) );
 214: 	ASSERT( 0 < bufsize );
 215: 	ASSERT( IsValidStringBufferPtr( buffer, bufsize ) );
 216: 	DESTROY_TEXT_BUFFER( buffer, bufsize );
 217: 
 218: 	//
 219: 	BYTE sha512[ SHA512_HASHSIZE ];
 220: 	VERIFY( SHA512_GetHash( context, sha512, sizeof( sha512 ) ) );
 221: 
 222: 	//
 223: 	char text[ 1+SHA512_HASHTEXTSIZE ];
 224: 	{
 225: 		char *p = text;
 226: 		{for( int i = 0; i < numof( sha512 ); i += sizeof( DWORD64 ) )
 227: 		{
 228: 			{for( int j = 0; j < sizeof( DWORD64 ); ++j )
 229: 			{
 230: 				snprintf( p, numof(text) - (p - text), "%02x", sha512[ i + (sizeof( DWORD64 ) - j - 1) ] );
 231: 				p = strtail( p );
 232: 			}}
 233: 		}}
 234: 	}
 235: 
 236: 	//
 237: 	memcpy( buffer, text, min( bufsize, sizeof( text ) ) );
 238: 	buffer[ min( bufsize, numof( text ) ) - 1 ] = '\0';
 239: 
 240: 	return true;
 241: }//SHA512_GetHashText
 242: 
 243: 
 244: //******************************************************************************************************************
 245: //
 246: //******************************************************************************************************************
 247: //
 248: 
 249: const DWORD64 c_qwK[ SHA512_WORK ] =
 250: {
 251: 	0x428a2f98d728ae22, 0x7137449123ef65cd, 0xb5c0fbcfec4d3b2f, 0xe9b5dba58189dbbc,
 252: 	0x3956c25bf348b538, 0x59f111f1b605d019, 0x923f82a4af194f9b, 0xab1c5ed5da6d8118,
 253: 	0xd807aa98a3030242, 0x12835b0145706fbe, 0x243185be4ee4b28c, 0x550c7dc3d5ffb4e2,
 254: 	0x72be5d74f27b896f, 0x80deb1fe3b1696b1, 0x9bdc06a725c71235, 0xc19bf174cf692694,
 255: 	0xe49b69c19ef14ad2, 0xefbe4786384f25e3, 0x0fc19dc68b8cd5b5, 0x240ca1cc77ac9c65,
 256: 	0x2de92c6f592b0275, 0x4a7484aa6ea6e483, 0x5cb0a9dcbd41fbd4, 0x76f988da831153b5,
 257: 	0x983e5152ee66dfab, 0xa831c66d2db43210, 0xb00327c898fb213f, 0xbf597fc7beef0ee4,
 258: 	0xc6e00bf33da88fc2, 0xd5a79147930aa725, 0x06ca6351e003826f, 0x142929670a0e6e70,
 259: 	0x27b70a8546d22ffc, 0x2e1b21385c26c926, 0x4d2c6dfc5ac42aed, 0x53380d139d95b3df,
 260: 	0x650a73548baf63de, 0x766a0abb3c77b2a8, 0x81c2c92e47edaee6, 0x92722c851482353b,
 261: 	0xa2bfe8a14cf10364, 0xa81a664bbc423001, 0xc24b8b70d0f89791, 0xc76c51a30654be30,
 262: 	0xd192e819d6ef5218, 0xd69906245565a910, 0xf40e35855771202a, 0x106aa07032bbd1b8,
 263: 	0x19a4c116b8d2d0c8, 0x1e376c085141ab53, 0x2748774cdf8eeb99, 0x34b0bcb5e19b48a8,
 264: 	0x391c0cb3c5c95a63, 0x4ed8aa4ae3418acb, 0x5b9cca4f7763e373, 0x682e6ff3d6b2b8a3,
 265: 	0x748f82ee5defb2fc, 0x78a5636f43172f60, 0x84c87814a1f0ab72, 0x8cc702081a6439ec,
 266: 	0x90befffa23631e28, 0xa4506cebde82bde9, 0xbef9a3f7b2c67915, 0xc67178f2e372532b,
 267: 	0xca273eceea26619c, 0xd186b8c721c0c207, 0xeada7dd6cde0eb1e, 0xf57d4f7fee6ed178,
 268: 	0x06f067aa72176fba, 0x0a637dc5a2c898a6, 0x113f9804bef90dae, 0x1b710b35131c471b,
 269: 	0x28db77f523047d84, 0x32caab7b40c72493, 0x3c9ebe0a15c9bebc, 0x431d67c49c100d4c,
 270: 	0x4cc5d4becb3e42b6, 0x597f299cfc657e2a, 0x5fcb6fab3ad6faec, 0x6c44198c4a475817,
 271: };
 272: 
 273: static inline DWORD64 ROTATE_LEFT( DWORD64 x, DWORD n ) { return (x >> n) | (x << (64 - n)); }
 274: static inline DWORD64 Ch(DWORD64 x, DWORD64 y, DWORD64 z) {return (x & (y ^ z)) ^ z;}
 275: static inline DWORD64 Maj(DWORD64 x, DWORD64 y, DWORD64 z) {return (x & (y | z)) | (y & z);}
 276: static inline DWORD64 S0(DWORD64 x) {return ROTATE_LEFT(x, 28) ^ ROTATE_LEFT(x, 34) ^ ROTATE_LEFT(x, 39);}
 277: static inline DWORD64 S1(DWORD64 x) {return ROTATE_LEFT(x, 14) ^ ROTATE_LEFT(x, 18) ^ ROTATE_LEFT(x, 41);}
 278: static inline DWORD64 s0(DWORD64 x) {return ROTATE_LEFT(x,  1) ^ ROTATE_LEFT(x,  8) ^ (x >> 7);}
 279: static inline DWORD64 s1(DWORD64 x) {return ROTATE_LEFT(x, 19) ^ ROTATE_LEFT(x, 61) ^ (x >> 6);}
 280: 
 281: 
 282: //*********************************************************
 283: // SHA512_IsValidContext
 284: //*********************************************************
 285: static
 286: bool
 287: SHA512_IsValidContext
 288: 	(
 289: 		const SHA512_CTX *context
 290: 	)
 291: {
 292: 	VALID_TEST( context );
 293: 	VALID_TEST( IsValidReadPtr( context, sizeof( *context ) ) );
 294: 	VALID_TEST( (0 <= context->index) && (context->index < sizeof( context->buffer ) ) );
 295: 
 296: 	return true;
 297: }//SHA512_IsValidContext
 298: 
 299: //*********************************************************
 300: // SHA512_Transform
 301: //*********************************************************
 302: static
 303: void
 304: SHA512_Transform
 305: 	(
 306: 		      DWORD64 *state,
 307: 		const DWORD64 *buffer
 308: 	)
 309: {
 310: 	DWORD64 work[ SHA512_WORK ];
 311: 	{for( int i = 0; i < SHA512_BLOCK; ++i )
 312: 	{
 313: 		work[ i ] = ReverseEndian64( buffer[ i ] );
 314: 	}}
 315: 	{for( int i = SHA512_BLOCK; i < SHA512_WORK; ++i )
 316: 	{
 317: 		work[ i ] = s1( work[ i - 2 ] ) + work[ i - 7 ] + s0( work[ i - 15 ] ) + work[ i - 16 ];
 318: 	}}
 319: 
 320: 	DWORD64 Hash[ SHA512_WORK + SHA512_HASH ];
 321: 	DWORD64 *pHash = &Hash[ SHA512_WORK ];
 322: 	memcpy( pHash, state, SHA512_HASH * sizeof( DWORD64 ) );
 323: 
 324: 	{for( int i = 0; i < SHA512_WORK; ++i )
 325: 	{
 326: 		pHash--;
 327: 		DWORD64 qwT1 = pHash[8] + S1( pHash[5] ) + Ch( pHash[5], pHash[6], pHash[7] ) + c_qwK[ i ] + work[ i ];
 328: 		DWORD64 qwT2 = S0( pHash[1] ) + Maj( pHash[1], pHash[2], pHash[3] );
 329: 		pHash[ 4 ] += qwT1;
 330: 		pHash[ 0 ]  = qwT1 + qwT2;
 331: 	}}
 332: 
 333: 	{for( int i = 0; i < SHA512_HASH; ++i )
 334: 	{
 335: 		state[ i ] += Hash[ i ];
 336: 	}}
 337: }//SHA512_Transform
 338: 
 339: 
 340: //******************************************************************************************************************
 341: // TEST
 342: //******************************************************************************************************************
 343: 
 344: 
 345: #ifdef _DEBUG // デバッグ時のみ
 346: 
 347: 
 348: //*********************************************************
 349: // test_sha512
 350: //*********************************************************
 351: DEFINE_TESTPROC( test_sha512 )
 352: {
 353: 	//---------------------------------------------------------
 354: 	// 定数 の テスト
 355: 	//---------------------------------------------------------
 356: 
 357: 	//---------------------------------------------------------
 358: 	// ファイルスコープ関数 の テスト
 359: 	//---------------------------------------------------------
 360: 
 361: 	//---------------------------------------------------------
 362: 	// 公開関数 の テスト
 363: 	//---------------------------------------------------------
 364: 
 365: 	static const BYTE testcase[] = 
 366: 		{
 367: 			0x00, 0x11, 0x22, 0x33, 0x44, 0x55, 0x66, 0x77, 0x88, 0x99, 0xaa, 0xbb, 0xcc, 0xdd, 0xee, 0xff, 0x00, 0x11, 0x22, 0x33, 0x44, 0x55, 0x66, 0x77, 0x88, 0x99, 0xaa, 0xbb, 0xcc, 0xdd, 0xee, 0xff, 0x00, 0x11, 0x22, 0x33, 0x44, 0x55, 0x66, 0x77, 0x88, 0x99, 0xaa, 0xbb, 0xcc, 0xdd, 0xee, 0xff, 0x00, 0x11, 0x22, 0x33, 0x44, 0x55, 0x66, 0x77, 0x88, 0x99, 0xaa, 0xbb, 0xcc, 0xdd, 0xee, 0xff, 
 368: 			0xbc, 0x76, 0xb4, 0xaf, 0x6b, 0xa5, 0xd4, 0x7e, 0xfc, 0xbf, 0xdc, 0x24, 0x77, 0x84, 0x82, 0xa5, 0xc9, 0x7a, 0x45, 0x18, 0x02, 0xbe, 0x2c, 0x0e, 0xcc, 0x52, 0x35, 0x22, 0xdd, 0x6f, 0x85, 0x79, 0x51, 0x3d, 0xdf, 0xf1, 0xe6, 0x4f, 0x5c, 0xfa, 0x7d, 0x60, 0x09, 0x0a, 0x14, 0xd0, 0x78, 0x9d, 0xa9, 0x31, 0x4a, 0x1b, 0x43, 0x2d, 0x55, 0x21, 0xf3, 0xea, 0xf0, 0x66, 0xf1, 0x74, 0x4c, 0x95, 
 369: 			0x5e, 0x8c, 0x5b, 0xe6, 0xab, 0xda, 0x8c, 0x06, 0x3c, 0xb6, 0x18, 0x69, 0x22, 0xbc, 0x4d, 0xaa, 0x7c, 0x9c, 0xae, 0xea, 0x14, 0x86, 0xe0, 0xd5, 0xb1, 0xe0, 0x0a, 0x26, 0x16, 0xc8, 0xf9, 0xea, 0x13, 0x4a, 0xec, 0x6a, 0xb1, 0x6d, 0x70, 0xf8, 0x1d, 0x05, 0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x15, 0x2c, 0xe3, 0xb2, 0xf4, 0x15, 0xed, 0x85, 0xff, 0x7b, 0x05, 0x89, 0x55, 0x92, 0x41, 0xc3, 0xc9, 0x3b, 0xae, 0x3e, 0x3c, 0x15, 0x4b, 0x3b, 0x38, 0xad, 0x21, 0x1c, 0x47, 0x44, 0x0e, 0xe1, 0xea, 0xa4, 0x3f, 0xff, 0x05, 0x18, 0xba, 0xb2, 0x14, 0x5f, 0xd2, 0xee, 0x24, 0xad, 0xac, 0xb3, 0x2c, 0xa4, 0x68, 0x70, 0xfa, 
 379: 			0xa2, 0x1e, 0x19, 0x0d, 0xec, 0xf2, 0x72, 0x53, 0x94, 0x39, 0x58, 0xf5, 0x85, 0x85, 0x61, 0x5e, 0x7c, 0xf3, 0xe9, 0x86, 0x32, 0x6b, 0x10, 0x10, 0x35, 0x73, 0xa6, 0x74, 0xd8, 0x4d, 0x25, 0xc5, 0x81, 0xbd, 0x2a, 0x81, 0x21, 0x0d, 0x13, 0x42, 0x6d, 0xdd, 0x4f, 0x8b, 0x6b, 0xa4, 0x0b, 0xea, 0xef, 0x23, 0x30, 0x8e, 0xd8, 0xa7, 0x54, 0x72, 0xfe, 0x40, 0x4c, 0x87, 0x38, 0xde, 0x40, 0x24, 
 380: 			0x92, 0xba, 0xda, 0x87, 0x42, 0x9f, 0x8f, 0x52, 0x92, 0xc5, 0xef, 0xdf, 0xf4, 0xcc, 0xb5, 0x04, 0x0d, 0x64, 0xa8, 0x97, 0xd2, 0x18, 0x41, 0x39, 0x4c, 0xf5, 0xcd, 0x7e, 0xc7, 0x8c, 0x24, 0xa3, 0x47, 0x0b, 0xbd, 0x53, 0xae, 0xb5, 0x11, 0x47, 0x98, 0xe7, 0xe9, 0xab, 0x29, 0xdd, 0x2e, 0xd5, 0x24, 0x04, 0xce, 0x11, 0x1d, 0xbd, 0xcb, 0x20, 0x5e, 0xe1, 0x41, 0xf3, 0x7d, 0x64, 0xf1, 0xf4, 
 381: 			0x59, 0x82, 0xdb,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 0x67, 0x70, 0x62, 0xf9, 0xc8, 0xe4, 0x78, 0x36, 0x54, 0x24, 0x5e, 0x2e, 0x35, 0x33, 0x4e, 0xff, 0x89, 0xc8, 0xd3, 0xda, 0xea, 0x70, 0xf9, 0x17, 0x8b, 0x96, 0x73, 0x70, 0x2a, 0xca, 0xa3, 0x06, 
 390: 			0x63, 0xc5, 0xe4, 0xee, 0x9f, 0x39, 0x80, 0x16, 0x77, 0xcc, 0x4a, 0xd7, 0x50, 0x98, 0xca, 0x9f, 0xf1, 0xbe, 0x7a, 0xae, 0x3a, 0x88, 0xc8, 0xa7, 0x13, 0x8b, 0xce, 0x21, 0xbc, 0xcb, 0x8e, 0x7d, 0x24, 0xc9, 0x4f, 0x94, 0x85, 0x74, 0xb0, 0x67, 0x84, 0x7b, 0xeb, 0x6b, 0xa5, 0x99, 0xc5, 0x89, 0x3f, 0x7b, 0x83, 0x00, 0x4b, 0x12, 0xc8, 0x20, 0xfe, 0x12, 0x43, 0xad, 0x34, 0x93, 0xb3, 0xbe, 
 391: 			0x62, 0x81, 0x88, 0x6f, 0x53, 0xd5, 0xeb, 0x91, 0x0f, 0x3b, 0xac, 0xbc, 0x76, 0xd3, 0xd4, 0x15, 0x26, 0x00, 0x64, 0xe9, 0xc4, 0x20, 0x43, 0x85, 0xde, 0x03, 0x59, 0x04, 0x2e, 0xc1, 0x40, 0x1f, 0xcb, 0x48, 0x4a, 0xd3, 0x14, 0xbf, 0x6b, 0x70, 0x35, 0x89, 0x8c, 0xdc, 0xef, 0x57, 0x5f, 0xc4, 0x1b, 0x2c, 0x98, 0xcb, 0x8a, 0xf9, 0x60, 0x7a, 0xaa, 0x23, 0xdd, 0x5b, 0xa6, 0x6c, 0x13, 0xd3, 
 392: 			0x63, 0x39, 0xfa, 0x9e, 0x19, 0xa0, 0x8b, 0x32, 0x0b, 0x46, 0xcf, 0xbd, 0x18, 0xfb, 0x5a, 0x21, 0x78, 0xaa, 0xb4, 0xad, 0xb7, 0x65, 0xde,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398: 			0x07, 0xe3, 0xbf, 0x9b, 0xa5, 0x4a, 0x43, 0xb4, 0xb3, 0xf7, 0x9f, 0xb3, 0xc5, 0xcc, 0xac, 0x2e, 0xae, 0xf1, 0x8b, 0xd0, 0x91, 0x13, 0xf1, 0x43, 0xfd, 0xbe, 0xed, 0x25, 0x9e, 0xdd, 0x6a, 0x9a, 0xc3, 0x2f, 0xce, 0x07, 0x46, 0xde, 0x5e, 0xe6, 0x33, 0x34, 0x16, 0x60, 0x49, 0xba, 0xaa, 0x43, 0x66, 0x71, 0xc0, 0xb2, 0x0f, 0xbf, 0x4f, 0xfc, 0x95, 0x27, 0xc8, 0x39, 0x18, 0x03, 0xa0, 0x7b, 
 399: 		};
 400: 
 401: 	// SHA512
 402: 	SHA512_CTX *context = SHA512_CreateAlgorithm();
 403: 
 404: 	// 一気にハッシュ
 405: 	{for( int i = 1; (i * SHA512_HASHSIZE) < numof( testcase ); ++i )
 406: 	{
 407: 		SHA512_InitData( context );
 408: 		SHA512_AddData( context, testcase, i * SHA512_HASHSIZE );
 409: 
 410: 		BYTE sha[ SHA512_HASHSIZE ];
 411: 		{for( int j = 1; j < numof( sha ); ++j )
 412: 		{
 413: 			SHA512_GetHash( context, sha, j );
 414: 			VERIFY( 0 == memcmp( sha, testcase + (i * SHA512_HASHSIZE), j ) );
 415: 		}}
 416: 	}}
 417: 
 418: 	// 徐々にハッシュ
 419: 	SHA512_InitData( context );
 420: 	{for( int i = 1; (i * SHA512_HASHSIZE) < numof( testcase ); ++i )
 421: 	{
 422: 		{for( int j = 0; j < SHA512_HASHSIZE; ++j )
 423: 		{
 424: 			SHA512_AddData( context, &testcase[ j + ((i-1) * SHA512_HASHSIZE) ], 1 );
 425: 		}}
 426: 
 427: 		BYTE sha[ SHA512_HASHSIZE ];
 428: 		SHA512_GetHash( context, sha, numof( sha ) );
 429: 		VERIFY( 0 == memcmp( sha, testcase + (i * SHA512_HASHSIZE), numof( sha ) ) );
 430: 	}}
 431: 
 432: 	VERIFY( SHA512_DestroyAlgorithm( context ) );
 433: 
 434: }//test_sha512
 435: 
 436: 
 437: #endif // #ifdef _DEBUG
 438: 
 439: 
 440: //** end **
 441: 

参照:


Google
ご意見・ご感想をお聞かせ下さい。匿名で送信できます。

 * 返信が必要な場合には postmaster@katsura-kotonoha.sakura.ne.jp へ直接メールしてください。

水無瀬の部屋 > sample > tools > misc > sha512.cpp

このページは cpp2web が出力しました。
水無瀬 優 postmaster@katsura-kotonoha.sakura.ne.jp
http://katsura-kotonoha.sakura.ne.jp/prog/code/tools/misc/sha512_cpp.shtml
『新妻LOVELY×CATION』を応援しています!